﻿<?php include $this->admin_tpl('meta');?>
<title>订做列表</title>
<script type="text/javascript" src="<?php echo STATIC_URL;?>plugin/jquery/1.8.2/jquery.min.js"></script>
<meta name="referrer" content="no-referrer"> 
</head>
<body>
<div class="page-container" style="padding-bottom:0;">
	<form id="search">
		<div class="btn-group c">
			<span class="btn <?php if($brand=='JEEP'){ echo 'btn-primary';}else{ echo 'btn-default';}?>  radius">JEEP</span>
			<span class="btn <?php if($brand=='马克华菲'){ echo 'btn-primary';}else{ echo 'btn-default';}?> radius">马克华菲</span>
			<span class="btn <?php if($brand=='增致牛仔 ZENGZHI'){ echo 'btn-primary';}else{ echo 'btn-default';}?> radius">佑晟订做</span>
			<!--<span class="btn <?php if($brand=='东方骆驼'){ echo 'btn-primary';}else{ echo 'btn-default';}?> radius">东方骆驼</span>
			<span class="btn <?php if($brand=='衣品天成'){ echo 'btn-primary';}else{ echo 'btn-default';}?> radius">衣品天成</span>-->
		</div>
		<input type="hidden" name="brand" value="<?php echo $brand;?>">
		<input type="hidden" value="" name="field">
		<input type="hidden" value="" name="order">
		<input type="hidden" value="0" name="download">
		<div class="c"><input type="text" name="other_2" class="input-text" style="width:170px" placeholder="其他属性2" value="" autocomplete="off"></div>
		<div class="c"><input type="text" name="sku_name" class="input-text" style="width:170px" placeholder="商品名称" value="" autocomplete="off"></div>
		<div class="c">
			<span class="select-box inline">
			<select name="buhuo_by_days" class="select">
				<option value="2" <?php if($buhuo_by_days == '2'){ echo 'selected'; } ?>>按2天销量统计补货</option>
				<option value="3" <?php if($buhuo_by_days == '3'){ echo 'selected'; } ?>>按3天销量统计补货</option>
				<option value="7" <?php if($buhuo_by_days == '7'){ echo 'selected'; } ?>>按7天销量统计补货</option>
				<option value="30" <?php if($buhuo_by_days == '30'){ echo 'selected'; } ?>>按30天销量统计补货</option>
			</select>
			</span>
		</div>
		<div class="c" id="brand_select" style="display:none;">
			<span class="select-box inline">
			<select name="brand_select" class="select brand_select">
				<option value="ALL">佑晟定做全部品牌</option>
				<option value="增致牛仔 ZENGZHI">增致牛仔 ZENGZHI</option>
				<option value="衣品天成">衣品天成</option>
				<option value="东方骆驼">东方骆驼</option>
				<option value="美丽衣橱">美丽衣橱</option>
				<option value="IT">IT</option>
			</select>
			</span>
		</div>
		<a class="btn btn-success radius" href="javascript:void(0)" id="btn_search"><i class="yzm-iconfont yzm-iconsousuo"></i> 搜索</a>
		<button class="btn btn-primary radius" id="btn_reset" type="reset"><i class="yzm-iconfont yzm-iconshuaxin"></i> 重置</button>
		<button name="dosubmit" class="btn btn-warning radius hidden-xs" type="button" id="btn_download" url="<?php echo U('dingzuo');?>"><i class="yzm-iconfont yzm-iconxiazai"> 导出</i></button>
	</form>
	<table id="table" class="mt-20" lay-filter="table" style="font-size:12px;"></table>
</div>
<script type="text/html" id="tool_temp">
	<a href="javascript:;" class="btn btn-primary radius" id="btn_sales_by_sku"><i class="yzm-iconfont yzm-iconxuanzhuan-1"></i> 重新统计销量</a>
</script>
<script type="text/html" id="purchaser">
{{#  if(d.bcp_purchase_qty <1){ }}
{{= d.bcp_purchase_qty}}
{{# }else{ }}
<a target="_blank" class="c-primary purchaser" other_2="{{= d.kuanhao}}" data_url="<?php echo U('jst_admin/purchaser');?>?other_2={{= encodeURIComponent(d.kuanhao)}}" href="javascript:">{{= d.bcp_purchase_qty}}</a>
{{# } }}
</script>
<?php include $this->admin_tpl('jst_common/common_tpl');?>
<?php include $this->admin_tpl('footer');?>
<script type="text/javascript" src="<?php echo STATIC_URL;?>plugin/layui/layui.js"></script>
<script>
var db = 'jst_sku_sales';
function purchaser(){
	$('.purchaser').on('click',function(event){
		event.stopPropagation(); // 阻止事件冒泡
		let width = $(window).width()*0.8+'px';
		let height = $(window).height()*0.8+'px';
		if(screen.width<1200){
			width = '100%';
		}
		let other_2 = $(this).attr('other_2');
		if(!other_2){
			return false;
		}
		let url = $(this).attr('data_url');
		layer.open({  
			type: 2,  
			title: other_2+'采购单',  
			shadeClose: true,  
			shade: 0.2,  
			area: [width, height],  
			content: url,
			success: function(layero, index){
				
			}
		})
		return false;
	})
}
function sales_by_sku(key){
	var arr=['1days','2days','3days','5days','7days','30days'];
	var days = arr[key];
	if(days == '1days'){
		var msg = '正在统计昨日销量...';
	}
	if(days == '2days'){
		var msg = '正在统计2天销量...';
	}
	if(days == '3days'){
		var msg = '正在统计3天销量...';
	}
	if(days == '5days'){
		var msg = '正在统计5天销量...';
	}
	if(days == '7days'){
		var msg = '正在统计7天销量...';
	}
	if(days == '30days'){
		var msg = '正在统计30天销量...';
	}
	layer.msg(msg, {icon:16, time:0, shade:0.3});
	$.ajax({
		type: 'get',
		url: '<?php echo U('api/jst_timer/sales_by_sku');?>', 
		data: {days:days},
		dataType: "json", 
		success: function (res) {
			layer.closeAll();
			if(res.status){
				layer.msg(res.message, {icon:1, time:2000},function(){
					//授权成功后刷新页面
					key++;
					if(key < arr.length){
						sales_by_sku(key);
					}else{
						location.href = location.href;
					}
				}); 
			}else{
				layer.msg(res.message, {icon:2});
			}
		}
	})
}
layui.use(function(){ //亦可加载特定模块：layui.use(['layer', 'laydate', function(){
	table = layui.treeTable; //表格
	util = layui.util; //工具集
	dropdown = layui.dropdown; //下拉菜单
	form = layui.form;
	//var index = layer.load(1);
	//执行一个 table 实例
	option = {
		elem: '#table',
		id:'table',
		height: 'full-80',
		limit:30,
		limits:[30,60,90],
		url:'<?php echo U('dingzuo');?>',
		where: $('#search').serializeJson(),
		title: '商品列表',
		method:'post',
		autoSort: false,
		tree:{
			view:{
				indent:5,
				showIcon:false,
				flexIconClose:'',
				flexIconOpen:'',
				//iconLeaf:'<i class="layui-icon layui-icon-subtraction"></i>',
				
			}
		},
		totalRow: true, // 开启合计行
		page: {theme:'#409eff',groups:3,hash:true}, //开启分页
		toolbar: '#tool_temp', //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
		cols: [[ //表头
			{field: 'id', title: 'ID', width:40,align:'center'},
			{field: 'pic', title: '图片', width:40,align:'center',templet: '#pic_tpl'},
			{field: 'kuanhao', title: '其他属性2', width:180,align:''},
			{field: 'name', title: '商品名称/半成品商品编码',align:'',totalRow:'<div style="text-align:center">小计</div>'},
			{field: '1days', title: '昨天销量',sort:true, width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: '2days', title: '2天销量',sort:true, width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: '3days', title: '3天销量',sort:true, width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: '7days', title: '7天销量',sort:true, width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: '30days', title: '30天销量',sort:true, width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: 'trend', title: '趋势', width:60,align:'center',templet: function(row){
					if(row.trend == 0){
						text = '持平';
						txt = '平';
						this_class = '';
					}else if(row.trend>0){
						this_class = 'label-danger';
						text = '上升'+row.trend+'%';
						txt = '升';
					}else{
						this_class = 'label-success';
						text = '下降'+row.trend+'%';
						txt = '降';
					}
					return  '<span onmouseover="yzm_tips=layer.tips(\''+text+'，点击查看详情\', this)" onmouseout="layer.close(yzm_tips);"><a href="javascript:;" class="other_2" other_2="'+row.kuanhao+'" title="查看统计"><span class="label radius '+this_class+'">'+txt+'</span></span></span>';
				}
			},
			{field: 'inventorys', title: '库存', width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: 'sales_day', title: '库存可售天数', width:80,align:'center'},
			{field: 'bcp_purchase_qty', title: '半成品采购中', width:80,align:'center',templet:'#purchaser',totalRow:true,totalRowDecimals:0},
			{field: 'cp_purchase_qty', title: '成品加工中', width:80,align:'center',totalRow:true,totalRowDecimals:0},
			{field: 'bcp_in_qty', title: '半成品待入库', width:120,align:'center',totalRow:true,totalRowDecimals:0},
			{field: 'buhuo_total_qty', title: '建议补货', width:120,align:'center',totalRow:true,totalRowDecimals:0},
			{field: 'buhuo_cycel', title: '补货周期', width:80,align:'center'},
			{field: 'sales_add_time', title: '统计时间', align:'center', width:120,templet: function(row) {return util.toDateString(row.sales_add_time*1000, 'yy-MM-dd HH:mm:ss');}},
		]],
		done:function(res,curr,count){
			var options = this;
			//layer.close(index);
			public_table(db);
			purchaser();
			$('.btn-group span').click(function(){
				if($(this).hasClass('btn-primary')){
					return;
				}else{
					$('.btn-group').find('span').removeClass('btn-primary');
					$(this).removeClass('btn-default');
					$(this).addClass('btn-primary');
					var brand = $(this).html();
					if(brand == 'JEEP'){
						$('input[name="brand"]').val(brand);
					}if(brand == '马克华菲'){
						$('input[name="brand"]').val(brand);
					}else{
						$('input[name="brand"]').val('ALL');
					}
					table.reload('table',{where:$('#search').serializeJson()});
				}
				if($(this).html()!='JEEP'&&$(this).html()!='马克华菲'){
					$('#brand_select').show();
				}else{
					$('#brand_select').hide();
					$('.brand_select').val('');
				}
			});
			$('.brand_select').on('change',function(){
				$('input[name="brand"]').val($('.brand_select').val());
				table.reload('table',{where:$('#search').serializeJson()});
			})
			$('.other_2').on('click',function(){
				let width = '800px';
				if(screen.width<800){
					width = '100%';
				}
				let other_2 = $(this).attr('other_2');
				let url = '<?php echo U('jst_admin/trend',['type'=>'other_2'])?>'+'?kuan='+other_2;
				layer.open({  
					type: 2,  
					title: other_2+'销量统计',  
					shadeClose: true,  
					shade: 0.2,  
					area: [width, '600px'],  
					content: url,
					success: function(layero, index){
						
					}
				})
				return false;
			})
		}
	};
	
	// 监听节点点击事件
	table.on('row(table)', function(obj){
		// 当节点被点击时，自动展开该节点
		var data = obj.data; // 获取当前点击的节点数据
		var state = obj.state; // 获取当前节点的状态（开闭）
		// 如果节点是关闭的，则打开它
		// 调用 expandNode 方法来展开节点，该方法是 treeTable 模块提供的
		table.expandNode('table', {index:obj.index,expandFlag:state});
		$('div.layui-table-body table tbody ').find('tr[data-level="1"]').find('td').css({"background":"#f5fafe"});
		$('table').off('click', 'tr[data-level="1"] .other_2');
		$('table').on('click', 'tr[data-level="1"] .other_2', function() { 
			let width = '800px';
			if(screen.width<800){
				width = '100%';
			}
			let other_2 = $(this).attr('other_2');
			let url = '<?php echo U('jst_admin/trend',['type'=>'other_2'])?>'+'?kuan='+other_2;
			layer.open({  
				type: 2,  
				title: other_2+'销量统计',  
				shadeClose: true,  
				shade: 0.2,  
				area: [width, '600px'],  
				content: url,
				success: function(layero, index){
					
				}
			})
			return false;
		})
	});
	table.render(option);
	init_table(db);
	$('#btn_sales_by_sku').click(function(){
		sales_by_sku(0);
	});
})
</script>
</body>
</html>